/**
 * @author user
 */

function VatataStatusClass(){
	//check timer
	// const TIME_CHECK_NETWORK_INFO = 30000;
	// const TIME_CHECK_DEVICES_INFO = 100000;

	const TIME_CHECK_NETWORK_INFO = 5000;
	const TIME_CHECK_DEVICES_INFO = 5000;
	
	//init wae
	var m_waeDevice = null;
	var m_waeNetInfo = null;
	var m_waeNetworkManager = null;
	
	//init id
	var m_netTypeImgId = null;
	var m_remoteControlImgId= null;
	var m_tvataStatusImgId = null;
	var m_deviceNameId = null;
	
	//img
	const LOGO_WIFI_OFF = "images/publicinfo/wifi_off.png";
	var LOGO_WIFI_ON= "images/publicinfo/wifi_{SINGLE}.png";
	const LOGO_LAN_ON = "images/publicinfo/lan_on.png";
	const LOGO_LAN_OFF = "images/publicinfo/lan_off.png";
	const LOGO_TVATA_SERVER_ON = "images/publicinfo/tvata_server_on.png";
	const LOGO_TVATA_SERVER_OFF="images/publicinfo/tvata_server_off.png";
	const LOGO_REMOTER_ON="images/publicinfo/remoter_on.png";
	const LOGO_REMOTER_OFF="images/publicinfo/remoter_off.png";
	const LOGO_USER = "images/publicinfo/user.png";
	
	var m_checkNetworkInfoTimer = null;
	var m_checkDeviceInfoTimer = null;
	
	function initId(){
		m_netTypeImgId = $("netTypeImgId");
		m_remoteControlImgId = $("remoteControlImgId");
		m_tvataStatusImgId = $("tvataStatusImgId");
		m_deviceNameId = $("deviceNameId");
	}
	function initWae(){
		m_waeDevice = VatataPublic.createAndroidPlug("Device");
		m_waeNetInfo = VatataPublic.createAndroidPlug("Http");
		m_waeNetworkManager = VatataPublic.createAndroidPlug("NeworkManager");

	}
	function writeText(id,str){
		if(!str)
			return;
		id.textContent  = str;
	}
	function writeImage(id,logo){
		if(!logo)
			return;
		id.src = logo;
	}
	
	this.addNetChangedListener = function(callback){
		m_waeNetworkManager.onNetChanged  = function(e){
			var _netContent = e.arguments[0].toLocaleString();
			if(_netContent){
				var _fun = "("+callback+"('"+_netContent+"'))";
				eval(_fun);
			}
		}
	}
	this.getNetInfo = function(){getNetInfo()}
	function getNetInfo(){
		if(m_checkNetworkInfoTimer)
			clearTimeout(m_checkNetworkInfoTimer)
			
		var _networkInfo = m_waeNetworkManager.getTvataNetworkInfo();
		
		//alert(_networkInfo)
		const NET_ON=0;
		const NET_OFF = 1;
		
		if(!_networkInfo)
			return;
		
		var _netInfoJson  = eval("("+_networkInfo+")");
		var _type = _netInfoJson.type;
		var _single=_netInfoJson.strength;
		var _tvataServerStatus=_netInfoJson.tvataServerEnable;
		
		var _netTypeImg  =LOGO_LAN_ON;
		//mobile,wifi,eth,off
		if(_type=="wifi"){
			_netTypeImg =  LOGO_WIFI_ON.replace(/{SINGLE}/,_single);
		}else if(!_type||_type=="off"){
			_netTypeImg = LOGO_LAN_OFF;
		}
		
		//写出网络状态给其他人用
		writeNetStatus(_single);
		
		var _tvataStatusImg = LOGO_TVATA_SERVER_OFF;
		if(_tvataServerStatus||_tvataServerStatus=="true")
			_tvataStatusImg =LOGO_TVATA_SERVER_ON;
		
		writeImage(m_netTypeImgId,_netTypeImg);
		writeImage(m_tvataStatusImgId,_tvataStatusImg);
		
		m_checkNetworkInfoTimer = setTimeout(function(){getNetInfo();},TIME_CHECK_NETWORK_INFO);
	}
	function writeNetStatus(type){
		waeparams.addContentProviderPersistenceWithAutoCommit("NetworkStatus" , type) ;
	}
	this.getDeviceInfo = function(){getDeviceInfo();}
	function getDeviceInfo(){
		if(m_checkDeviceInfoTimer)
			clearTimeout(m_checkDeviceInfoTimer);
			
		//get device id
		var _devcieId = m_waeDevice.getTvataDeviceID();
		var _deviceName = m_waeDevice.getTvataDeviceName();
		//get device Remote controll status
		var _deviceControlStatus = m_waeDevice.getTvataRemoteControlStatus();
		//alert(_devcieId+"===="+_deviceControlStatus)
		writeText(m_deviceNameId,_deviceName);
		
		var _remoteStatusImg = LOGO_REMOTER_OFF; 
		if(_deviceControlStatus||_deviceControlStatus=="true")
			_remoteStatusImg =LOGO_REMOTER_ON;
		
		writeImage(m_remoteControlImgId,_remoteStatusImg);
		
		m_checkDeviceInfoTimer = setTimeout(function(){getDeviceInfo();},TIME_CHECK_DEVICES_INFO);
	}
	this.init = function(){
		
		initId();
		
		initWae();
		
		getNetInfo();
		getDeviceInfo();
	}
}
var VatataStatus = new VatataStatusClass();